<template>
    <div class="e-form">
       <BillTop @cancel="handleClose"></BillTop>
        <div class="tabs">
            <div class="tabs-title">基本信息</div>
            <el-form ref="form" :model="formData" label-width="160px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="机构：">
                         {{ formData.orgName }}
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="业务类型：">
                            <el-select
                                v-model="formData.businessTypeValue"
                                placeholder="请选择"
                                 @change="busTypeChange"
                            >
                                  <el-option
                                  v-for="(
                                    item
                                    ) in businessDtlLists"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                    >
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="发票号：">
                            <el-input v-model="formData.invoiceNo" v-enterInteger1>
                                 <span>{{formData.invoiceNo }}</span>
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="录入人员：">
                         {{formData.inputName}}
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="结算日期：" v-model="formData.settleDate">
                            <el-date-picker
                                v-model="formData.settleDate"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                      <el-col :span="12">
                        <el-form-item label="单据来源：">
                          新增
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="状态：" >
                           草稿
                        </el-form-item>
                    </el-col>
                      <el-col :span="12" class="redFont">
                        <el-form-item label="传财务共享：" >
                            未传
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="24">
                        <el-form-item label="备注：" >
                            <el-input placeholder="" v-model="formData.remarks" class="input-with-select" type="textarea"
                            :rows="3" maxlength='500'>
                            <span>
                                {{formData.remarks}}
                            </span>
                            </el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>
        <div class="buttons">
            <el-button
                type="primary"
                size="small"
                class="btn-blue"
                @click="preserve"
                v-debounce
                >保存</el-button
            >
            <el-button size="small" @click="handleClose"  v-debounce>取消</el-button>
        </div>
    </div>
</template>

<script>
import request from '@/api/settlementManagement/weeklyMaterialSettlement/purchaseSettlement.js'
import { mapState } from 'vuex'
import { formatDate } from '@/utils/common'
export default {
    data () {
        return {
            redBillStateStyle: {},
            businessDtlLists: [{         //业务类型
                value: '0',
                label: '物资采购合同',
            }, {
                value: '1',
                label: '临时需用计划'
            }, {
                value: '2',
                label: '调拨单'
            }],
            checkedBox: false,
            formData: {
                'billSource': 1,
                'businessTypeKey': 1,
                'businessTypeValue': '临时需用计划',
                'inputId': '',
                'inputName': '',
                'invoiceNo': '',
                'orgId': '',
                'orgName': '',
                'remarks': '',
                'settleDate': formatDate(new Date(), 'yyyy-MM-dd'), //结算日期,
                'state': 0,
                'transferFinancialShare': 0
            },
        }
    },
    computed: {
        ...mapState({
            userInfo: state => state.userInfo
        }),
        //传输财务共享
        shareLable () {
            return param=> {
                const state =  this.$store.state.equip.equipData.shareFrom
                const obj = state.find(x=>x.value === param)
                return (obj ?  obj.label :  '')
            }
        },
    },
    created () {
        const { orgInfo, userId, userName } = this.userInfo
        this.formData.orgId = orgInfo.orgId //机构id
        this.formData.orgName = orgInfo.orgName //机构名称
        this.formData.inputId = userId // 录入人Id
        this.formData.inputName = userName // 录入人名称
    },
    components: {
    },
    mounted () {
    },
    methods: {
        //选择业务类型
        busTypeChange (val) {
            const findObj = this.businessDtlLists.find(item => item.value == val)
            this.formData.businessTypeValue = findObj.label
            this.formData.businessTypeKey = findObj.value
        },
        //保存
        preserve () {
            const date = this.formData.settleDate + ' 00:00:00'
            request.isMonthly({ date, orgId: this.formData.orgId } )
                .then(res=>{
                    if(res.code == 200 && res.data == false) {
                        this.formData.businessTypeKey = Number(this.formData.businessTypeKey)
                        this.formData.redState = -1
                        this.formData.billSource = 0
                        request.addPurchaseSettlementData(this.formData)
                            .then(res=>{
                                this.clientPop('info', '保存成功!是否进行下一步操作？', ()=>{
                                    this.$router.replace({
                                        path: '/purchaseSettlementEdit',
                                        query: {
                                            billid: res,
                                        }
                                    })
                                },
                                null,
                                this.handleClose
                                )
                            })
                    }else {
                        this.$message.error('当前日期已办理月结请选择其他日期进行结算')
                    }
                })
        },
        //取消
        handleClose () {
            this.$router.replace('/purchaseSettlementList')
        },
    },
}
</script>

<style lang="scss" scoped>
.buttons {
    left: 0;
}
.e-form {
    padding: 0 20px;
    .tabs-title::before {
        content: '';
        height: 22px;
        width: 8px;
        border-radius: 40px;
        background-color: #2e61d7;
        display: block;
        position: absolute;
        left: 20px;
        margin-right: 20px;
    }
}
</style>
